<template>
    <div class="con-top">
        <!-- 轮播 -->
        <Slideshow interval="5000" style="width: 100%; height: 500px" />
    </div>
    <div class="categoryList">
        <div v-for="item in categoryList" :key="item.id" class="row">
            <span class="categor-title">{{ item.categoryName }}</span>
            <span
                v-for="subItem in item.children"
                @click="changeCategory(subItem)"
                :key="subItem.id"
                class="category-item"
                :class="{ 'category-item-active': subItem.is_active }"
                >{{ subItem.categoryName }}</span
            >
        </div>
    </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from "vue";
import Slideshow from "./Slideshow.vue";
import { getCategoryList } from "@/api/contest";
import { categoryListType } from "@/types/coutest";
const emit = defineEmits(["updateCurrentTypeDate"]);
import { useRoute } from "vue-router";
const route = useRoute();

function changeCategory(subItem: categoryListType) {
    categoryList.value.forEach(item => {
        item.children.forEach(sub => {
            sub.is_active = false;
        });
    });
    subItem.is_active = true;
    // 重新获取数据
    emit("updateCurrentTypeDate", subItem.id);
}
const categoryList = ref<categoryListType[]>([]);
onMounted(() => {
    const currentType = route.name as string;
    getCategoryList(currentType).then(res => {
        if (currentType === "活动") {
            categoryList.value = [
                {
                    categoryName: `${currentType}类别:`,
                    id: 0,
                    children: [{ categoryName: "全部", is_active: true, id: 0 }, ...res.data.data.reverse()],
                },
            ];
            return;
        }
        categoryList.value = [
            { categoryName: `${currentType}类别:`, id: 0, children: [{ categoryName: "全部", is_active: true }] },
            ...res.data.data.reverse(),
        ];
    });
});
</script>
<style lang="scss" scoped>
.con-top {
    display: flex;
    width: 1400px;
    margin: 0 auto;

    > .nav-left {
        width: 280px;
        background: #002625;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        position: relative;
        color: #fff;

        > .conitem {
            > .t-item {
                position: relative;
                padding: 10px 20px;
            }

            > .t-con {
                display: none;
                box-sizing: border-box;
                border-radius: 0px 10px 0px 0px;
                position: absolute;
                left: 280px;
                top: 0;
                padding: 20px;
                z-index: 999;
                width: 390px;
                height: 100%;
                background: #fff;
                color: #000;

                > .h4 {
                    font-size: 14px;
                    color: #333333;
                    font-weight: 700;
                }

                > a {
                    display: inline-block;
                    border-radius: 2px;
                    margin-right: 10px;
                    padding: 4px 10px;
                    margin-top: 10px;
                    color: #444950;
                }
            }

            &:hover {
                background-color: rgba(243, 245, 246, 0.7);

                > .t-con {
                    display: block;
                }
            }
        }
    }
}

.categoryList {
    width: 1200px;
    margin: 20px auto;
    padding: 10px;
    font-size: 16px;
    border: 1px solid #e6e6e6;
    border-radius: 10px;
    .row:nth-child(n + 2) {
        margin-left: 60px;
    }
    .row {
        margin: 20px 0;
        cursor: pointer;
    }

    .row:nth-child(1) {
        margin-bottom: 20px;
    }

    .row:nth-child(n + 2) {
        .category-item,
        .categor-title {
            color: #666;
        }
    }

    .categor-title {
        display: inline-block;
        width: 80px;
        text-align: right;
        color: #333 !important;
        font-weight: bold;
        font-size: 16px;
    }

    .category-item {
        display: inline-block;
        margin-left: 20px;
        border-radius: 2px;
        color: #898787;
        cursor: pointer;
        padding: 4px 16px;
        font-size: 14px;

        &:hover {
            color: $main-color !important;
        }
    }

    .category-item-active {
        color: $main-color !important;
        font-weight: 700;
        border-radius: 2px;
        background-color: #dde0eb;
        transition: all 0.1s ease;
    }
}
</style>
